package xyz.bali16.application.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.entity.UserRole;
import xyz.bali16.application.mapper.UserMapper;
import xyz.bali16.application.mapper.UserRoleMapper;
import xyz.bali16.application.service.UserRoleService;
import xyz.bali16.application.service.UserService;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    private final UserMapper userMapper;

    @Override
    public void add(String userId, String roleId) {
        if (roleId == null) {
            return;
        }
        //List<UserRole> userRoles = roleIds.stream()
        //        .filter(roleId -> !StrUtil.isEmpty(roleId))
        //        .map(roleId -> {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);

        //})
        //.collect(Collectors.toList());

        save(userRole);
    }

    @Override
    public void removeByRoleId(String roleId) {
        if (StrUtil.isEmpty(roleId)) {
            return;
        }
        Wrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getRoleId, roleId);
        remove(wrapper);
    }

    @Override
    public void removeByUserId(String userId) {
        if (StrUtil.isEmpty(userId)) {
            return;
        }
        Wrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getUserId, userId);
        remove(wrapper);
    }

    @Override
    public void removeByRoleId(String userId,String roleId) {
        if (StrUtil.isEmpty(userId)) {
            return;
        }
        if (roleId == null) {
            return;
        }
        Wrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getUserId, userId)
                .eq(UserRole::getRoleId, roleId);
        remove(wrapper);
    }

    @Override
    public List<UserRole> getByUserId(String userId) {
        if (StrUtil.isEmpty(userId)) {
            return Collections.emptyList();
        }
        Wrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getUserId, userId);
        return list(wrapper);
    }

    @Override
    public List<UserRole> getByRoleId(String roleId) {
        if (StrUtil.isEmpty(roleId)) {
            return Collections.emptyList();
        }
        Wrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getRoleId, roleId);
        return list(wrapper);
    }

    @Override
    public List<User> getUsersByRoleId(String roleId) {
        if (StrUtil.isEmpty(roleId)) {
            return Collections.emptyList();
        }
        Wrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery()
                .eq(UserRole::getRoleId, roleId);
        List<UserRole> userRoles = list(wrapper);
        List<String> ids = userRoles.stream().map(x -> x.getUserId()).collect(Collectors.toList());
        return userMapper.selectBatchIds(ids);
        //return userService.listByIds(ids);
    }

}
